From 4a3aaa110b1b2e4f534411474701bff3306e6a56 Mon Sep 17 00:00:00 2001 From: tsteven4 <13596209+tsteven4@users.noreply.github.com> Date: Thu, 24 Feb 2022 06:02:08 -0700 Subject: [PATCH] use Qt for os independent environmental variable access. (#856) --- defs.h | 3 --- gpsbabel-sample.ini | 9 --------- inifile.cc | 13 ++++++------- inifile.h | 1 - util.cc | 21 +++------------------ 5 files changed, 9 insertions(+), 38 deletions(-) diff --git a/defs.h b/defs.h index f08c9d013..f924ad4b2 100644 --- a/defs.h +++ b/defs.h @@ -1059,9 +1059,6 @@ FILE* xfopen(const char* fname, const char* type, const char* errtxt); // Thin wrapper around fopen() that supports Unicode fname on all platforms. FILE* ufopen(const QString& fname, const char* mode); -// OS-abstracting wrapper for getting Unicode environment variables. -QString ugetenv(const char* env_var); - // FIXME: case_ignore_strcmp() and case_ignore_strncmp() should probably // just be replaced at the call sites. These shims are just here to make // them more accommodating of QString input. diff --git a/gpsbabel-sample.ini b/gpsbabel-sample.ini index 743e9d704..d2feb0217 100644 --- a/gpsbabel-sample.ini +++ b/gpsbabel-sample.ini @@ -24,12 +24,3 @@ Prec = 6 [ gdb ] via = 1 - -;------------------------------------------------------------------ -[ tiger ] -snlen=7 - -;------------------------------------------------------------------ -[pathaway] -# dbname = The Last Trip -deficon = Golf Course diff --git a/inifile.cc b/inifile.cc index 404891cd1..cb0279071 100644 --- a/inifile.cc +++ b/inifile.cc @@ -18,7 +18,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ -#include "defs.h" // for fatal, ugetenv, warning +#include "defs.h" // for fatal, warning #include "inifile.h" #include "src/core/file.h" // for File #include // for QByteArray @@ -29,8 +29,7 @@ #include // for QHash #include // for QIODevice::ReadOnly, QIODevice #include // for QTextStream -#include // for CaseInsensitive -#include // for qPrintable +#include // for qEnvironmentVariable, qPrintable, QT_VERSION, QT_VERSION_CHECK #include #define MYNAME "inifile" @@ -66,7 +65,7 @@ open_gpsbabel_inifile() { QString res; - QString envstr = ugetenv("GPSBABELINI"); + QString envstr = qEnvironmentVariable("GPSBABELINI"); if (!envstr.isNull()) { if (QFile(envstr).open(QIODevice::ReadOnly)) { return envstr; @@ -79,9 +78,9 @@ open_gpsbabel_inifile() #ifdef __WIN32__ // Use &&'s early-out behaviour to try successive file locations: first // %APPDATA%, then %WINDIR%, then %SYSTEMROOT%. - (name = find_gpsbabel_inifile(ugetenv("APPDATA"))).isNull() - && (name = find_gpsbabel_inifile(ugetenv("WINDIR"))).isNull() - && (name = find_gpsbabel_inifile(ugetenv("SYSTEMROOT"))).isNull(); + (name = find_gpsbabel_inifile(qEnvironmentVariable("APPDATA"))).isNull() + && (name = find_gpsbabel_inifile(qEnvironmentVariable("WINDIR"))).isNull() + && (name = find_gpsbabel_inifile(qEnvironmentVariable("SYSTEMROOT"))).isNull(); #else // Use &&'s early-out behaviour to try successive file locations: first // ~/.gpsbabel, then /usr/local/etc, then /etc. diff --git a/inifile.h b/inifile.h index e1aa6afd4..ef42c0876 100644 --- a/inifile.h +++ b/inifile.h @@ -22,7 +22,6 @@ #define HAVE_INIFILE_H #include // for QHash -#include // for QList #include // for QString class InifileSection; diff --git a/util.cc b/util.cc index 0bae87a0e..a329463c7 100644 --- a/util.cc +++ b/util.cc @@ -26,7 +26,7 @@ #include // for va_list, va_end, va_start, va_copy #include // for size_t, vsnprintf, FILE, fopen, printf, sprintf, stderr, stdin, stdout #include // for uint32_t -#include // for abs, getenv, calloc, free, malloc, realloc +#include // for abs, calloc, free, malloc, realloc #include // for strlen, strcat, strstr, memcpy, strcmp, strcpy, strdup, strchr, strerror #include // for mktime, localtime @@ -44,7 +44,7 @@ #include // for QXmlStreamAttributes #include // for CaseInsensitive #include // for QTimeZone -#include // for qAsConst, QAddConst<>::Type, qPrintable +#include // for qAsConst, qEnvironmentVariableIsSet, QAddConst<>::Type, qPrintable #include "defs.h" #include "src/core/datetime.h" // for DateTime @@ -206,21 +206,6 @@ ufopen(const QString& fname, const char* mode) #endif } -/* - * OS-abstracting wrapper for getting Unicode environment variables. - */ -QString ugetenv(const char* env_var) -{ -#ifdef __WIN32__ - // Use QString to convert 8-bit env_var argument to wchar_t* for _wgetenv(). - return QString::fromWCharArray( - _wgetenv((const wchar_t*) QString(env_var).utf16())); -#else - // Everyone else uses UTF-8 or some other locale-specific 8-bit encoding. - return QString::fromLocal8Bit(std::getenv(env_var)); -#endif -} - /* * Allocate a string using a format list with optional arguments * Returns -1 on error. @@ -721,7 +706,7 @@ mklocaltime(struct tm* t) bool gpsbabel_testmode() { - static bool testmode = getenv("GPSBABEL_FREEZE_TIME") != nullptr; + static bool testmode = qEnvironmentVariableIsSet("GPSBABEL_FREEZE_TIME"); return testmode; } -- 2.30.2